package com.siloam.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.siloam.dao.NavDao;
import com.siloam.module.Navigation;
import com.siloam.vo.NavVO;
import com.siloam.vo.TreeNodeVO;

public class NavService {

	private NavDao navDao;

	public NavDao getNavDao() {
		return navDao;
	}

	public void setNavDao(NavDao navDao) {
		this.navDao = navDao;
	}
	
	@Transactional
	public void add(Navigation nav){
		navDao.add(nav);
	}
	
	@Transactional
	public void update(Navigation nav){
		navDao.update(nav);
	}
	
	@Transactional
	public void delete(String ids){
		String[] sid = ids.split(",");
		for(String id : sid){
			deleteOne(Integer.parseInt(id)) ;
		}
	}
	
	public Navigation load(int id){
		return navDao.load(id);
	}
	
	public List<NavVO> findFirstLevel(){
		List<Navigation> navs = navDao.findFirstLevel();
		List<NavVO> navVOS = new ArrayList<NavVO>();
		for(Navigation nav : navs){
			navVOS.add(new NavVO(nav));
		}
		return navVOS;
	}
	
	public List<NavVO> findFirstLevel(int type){
		List<Navigation> navs = navDao.findFirstLevel(type);
		List<NavVO> navVOS = new ArrayList<NavVO>();
		for(Navigation nav : navs){
			navVOS.add(new NavVO(nav));
		}
		return navVOS;
	}
	
	public List<NavVO> findSecondLevel(int firstId){
		List<Navigation> navs = navDao.findSecondLevel(firstId);
		List<NavVO> navVOS = new ArrayList<NavVO>();
		for(Navigation nav : navs){
			navVOS.add(new NavVO(nav));
		}
		return navVOS;
	}
	
	public void deleteOne(int id){
		Navigation nav = navDao.load(id);
		if(nav.getLevel() == Navigation.LV_FRIST){
			List<Navigation> navs = navDao.findSecondLevel(nav.getId());
			for(Navigation subNav : navs){
				navDao.delete(subNav.getId());
			}
		}
		navDao.delete(nav.getId());
	}
	
	/**
	 * 构建导航树
	 * @return
	 */
	public List<TreeNodeVO> buildNavTree(){
		TreeNodeVO backNode = new TreeNodeVO();
		backNode.setId(0);
		backNode.setText("后台导航");
		backNode.setChildren(buildNavNodes(Navigation.TYPE_MANAGER));

		Map<String,String> backAttr = new HashMap<String,String>();
		backAttr.put("level", "0");
		backAttr.put("type", "2");
		backNode.setAttributes(backAttr);
		
		TreeNodeVO frontNode = new TreeNodeVO();
		frontNode.setId(0);
		frontNode.setText("前台导航");
		frontNode.setChildren(buildNavNodes(Navigation.TYPE_SITE));
		Map<String,String> frontAttr = new HashMap<String,String>();
		frontAttr.put("level", "0");
		frontAttr.put("type", "1");
		frontNode.setAttributes(frontAttr);
		
		List<TreeNodeVO> roots = new ArrayList<TreeNodeVO>();
		roots.add(backNode);
		roots.add(frontNode);
		return roots;
	}
	
	public List<TreeNodeVO> buildNavNodes(int type){
		List<TreeNodeVO> firstLevelNodes = new ArrayList<TreeNodeVO>(); //第一层导航节点
		List<TreeNodeVO> secondLevelNodes ;//第二层导航节点
		
		List<Navigation> firstLevelNavs = navDao.findFirstLevel(type);//第一层导航
		List<Navigation> secondLevelNavs;//第二层导航
		
		Map<String,String> flAttrs = new HashMap<String,String>();//第一层导航属性
		flAttrs.put("level", "1");
		flAttrs.put("type", type+"");
		Map<String,String> slAttrs = new HashMap<String,String>();//第二层导航属性
		slAttrs.put("level", "2");
		slAttrs.put("type", type+"");
		for(Navigation nav : firstLevelNavs){
			TreeNodeVO flNode = new TreeNodeVO();
			flNode.setId(nav.getId());
			flNode.setText(nav.getName());
			flNode.setAttributes(flAttrs);
			secondLevelNavs = navDao.findSecondLevel(nav.getId(), type);
			secondLevelNodes = new ArrayList<TreeNodeVO>();
			for(Navigation sNav : secondLevelNavs){
				TreeNodeVO cNode = new TreeNodeVO();
				cNode.setId(sNav.getId());
				cNode.setText(sNav.getName());
				cNode.setAttributes(slAttrs);
				secondLevelNodes.add(cNode);
			}
			flNode.setChildren(secondLevelNodes);
			firstLevelNodes.add(flNode);
		}
		return firstLevelNodes;
	}
	
}
